package com.lv.common.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SizeUtils;
import com.lv.common.R;
import com.lv.common.utils.DensityUtil;

/**
 * 作者： ygx
 * 创建日期：2023/12/4 4:09 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
public class BorderView extends View {
    private Paint mPaint;
    private Paint mPaint2;
    private int mColor = Color.parseColor("#ef7b4a");
    private int mWidth = 5;
    private int mRedius = 2;
    private int mNRedius = 20;//内圆角
    private String mType = "0";//0 圆角 1 带斜边圆角
    private Context mContext;

    public BorderView(Context context) {
        this(context, null);
    }

    public BorderView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BorderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.BorderView);
        mWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.BorderView_borderWidth, mWidth);
        mRedius = obtainStyledAttributes.getDimensionPixelSize(R.styleable.BorderView_borderRedius, mRedius);
        mNRedius = obtainStyledAttributes.getDimensionPixelSize(R.styleable.BorderView_borderNRedius, mNRedius);
        mColor = obtainStyledAttributes.getColor(R.styleable.BorderView_borderColor, mColor);
        mWidth = DensityUtil.dip2px(context, mWidth);
        mRedius = DensityUtil.dip2px(context, mRedius);
        mNRedius = DensityUtil.dip2px(context, mNRedius);
        obtainStyledAttributes.recycle();
        initView();


    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mWidth);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setColor(mColor);


        mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint2.setStyle(Paint.Style.FILL);
        mPaint2.setColor(mColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int with = getWidth();
        int hight = getHeight();
        RectF rectF = new RectF(0, 0, with, hight);
        rectF.left = rectF.top = 0.5f * mWidth;
        rectF.right = getMeasuredWidth() - 0.5f * mWidth;
        rectF.bottom = getMeasuredHeight() - 0.5f * mWidth;
        canvas.drawRoundRect(rectF, mRedius, mRedius, mPaint);
        Path path = new Path();
        if (mType.equals("1")) {
            path.moveTo(0.5f * mWidth, 0.5f * mWidth);
            path.lineTo(getMeasuredWidth() - mNRedius - mWidth, mWidth);
            path.lineTo(0.5f * mWidth, DensityUtil.dip2px(mContext, 8.0f) + mWidth);
            path.lineTo(0.5f * mWidth, 0.5f * mWidth);
            canvas.drawPath(path, mPaint2);

        }
        path.moveTo(mWidth, mWidth);
        path.arcTo(new RectF(mWidth, mWidth, mNRedius + mWidth, mNRedius + mWidth), 180, 90);//左上
        path.lineTo(mWidth, mWidth);

        path.lineTo(getMeasuredWidth() - mWidth, mWidth);
        path.arcTo(new RectF(getMeasuredWidth() - mWidth - mNRedius, mWidth, getMeasuredWidth() - mWidth, mNRedius + mWidth), 270, 90);//右上
        path.lineTo(getMeasuredWidth() - mWidth, mWidth);

        path.lineTo(getMeasuredWidth() - mWidth, getMeasuredHeight() - mWidth);
        path.arcTo(new RectF(getMeasuredWidth() - mWidth - mNRedius, getMeasuredHeight() - mWidth - mNRedius, getMeasuredWidth() - mWidth, getMeasuredHeight() - mWidth), 90, -90);//右下
        path.lineTo(getMeasuredWidth() - mWidth, mWidth);

        path.moveTo(mWidth, getMeasuredHeight() - mWidth);
        path.arcTo(new RectF(mWidth, getMeasuredHeight() - mWidth - mNRedius, mNRedius + mWidth, getMeasuredHeight() - mWidth), 180, -90);//左下
        path.lineTo(mWidth, getMeasuredHeight() - mWidth);
        canvas.drawPath(path, mPaint2);


    }

    public void setColor(int color) {
        mColor = color;
        mPaint.setColor(mColor);
        mPaint2.setColor(mColor);
        invalidate();
    }

    public void setType(String type) {
        mType = type;
        invalidate();
    }

    public void setNRedius(String redius) {
        int r = 10;
        try {
            r = Integer.parseInt(redius);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mNRedius = DensityUtil.dip2px(mContext, r);
        invalidate();
    }

    public void setNRedius(int redius) {
        mNRedius = DensityUtil.dip2px(mContext, redius);
        invalidate();
    }
}
